#include <device.h>

#define CH_OFFSET 0

static uint8_t *uart_base = NULL;

static void uart_putc(char ch) {
  putc(ch, stderr);
}

static void uart_io_handler(uint32_t offset, int len, bool is_write) {
  assert(len == 1);
  switch (offset) {
    /* We bind the serial port with the host stderr in NEMU. */
    case CH_OFFSET:
      if (is_write) uart_putc(uart_base[0]);
      else {printf("do not support read"); assert(0);}
      break;
    default: printf("do not support offset = %d", offset); assert(0);
  }
}

void init_uart() {
  uart_base = new_space(8);
  add_mmio_map("uart", CONFIG_UART_BASE , uart_base, 8, uart_io_handler);
}
